<template>
  <!-- 根据路由元信息动态切换布局 -->
  <component 
    :is="getLayoutComponent" 
    v-if="currentLayout"
  >
    <router-view v-slot="{ Component, route }">
      <transition :name="getTransitionName(route)" mode="out-in">
        <component :is="Component" :key="route.path" />
      </transition>
    </router-view>
  </component>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()

// 根据路由元信息获取对应的布局组件
const currentLayout = computed(() => {
  const layout = route.meta.layout as string || 'WebAppLayout'
  return defineAsyncComponent(() => import(`@/components/layout/${layout}.vue`))
})

// 获取布局组件
const getLayoutComponent = computed(() => {
  switch (route.meta.layout) {
    case 'AdminLayout':
      return defineAsyncComponent(() => import('@/components/layout/AdminLayout.vue'))
    case 'AuthLayout':
      return defineAsyncComponent(() => import('@/components/layout/AuthLayout.vue'))
    case 'SimpleLayout':
      return defineAsyncComponent(() => import('@/components/layout/SimpleLayout.vue'))
    default:
      return defineAsyncComponent(() => import('@/components/layout/WebAppLayout.vue'))
  }
})

// 根据路由类型返回不同的过渡效果
const getTransitionName = (route: any) => {
  // 管理员端使用淡入淡出效果
  if (route.path.startsWith('/admin')) {
    return 'fade'
  }
  // 移动端使用滑动效果
  return 'slide'
}
</script>

<style>
/* 全局重置样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#app {
  height: 100%;
}

/* 移动端过渡效果 - 滑动 */
.slide-enter-active,
.slide-leave-active {
  transition: all 0.3s ease;
}

.slide-enter-from {
  transform: translateX(100%);
  opacity: 0;
}

.slide-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}

/* PC端过渡效果 - 淡入淡出 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 管理员端特定的样式 */
.admin-layout {
  background-color: #f0f2f5;
  min-height: 100vh;
}

/* 移动端特定的样式 */
.webapp-layout {
  background-color: #f5f5f5;
  min-height: 100vh;
  max-width: 100%;
  overflow-x: hidden;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .webapp-layout {
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* 防止页面缩放 */
@media (max-width: 768px) {
  body {
    -webkit-text-size-adjust: 100%;
  }
}

/* 自定义滚动条样式 */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>